package com.ruoyi.different.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.different.domain.DifferentLogging;
import com.ruoyi.different.service.IDifferentLoggingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 日志记录Controller
 * 
 * @author rocky
 * @date 2023-12-20
 */
@RestController
@RequestMapping("/different/logging")
public class DifferentLoggingController extends BaseController
{
    @Autowired
    private IDifferentLoggingService differentLoggingService;

    /**
     * 查询日志记录列表
     */
    @PreAuthorize("@ss.hasPermi('different:logging:list')")
    @GetMapping("/list")
    public TableDataInfo list(DifferentLogging differentLogging)
    {
        startPage();
        List<DifferentLogging> list = differentLoggingService.selectDifferentLoggingList(differentLogging);
        differentLoggingService.cheaks(list);
        return getDataTable(list);
    }

    /**
     * 导出日志记录列表
     */
    @PreAuthorize("@ss.hasPermi('different:logging:export')")
    @Log(title = "日志记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DifferentLogging differentLogging)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            throw new ServiceException("没有权限访问用户数据！");

        }
        List<DifferentLogging> list = differentLoggingService.selectDifferentLoggingList(differentLogging);
        ExcelUtil<DifferentLogging> util = new ExcelUtil<DifferentLogging>(DifferentLogging.class);
        util.exportExcel(response, list, "日志记录数据");
    }

    /**
     * 获取日志记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('different:logging:query')")
    @GetMapping(value = "/{logId}")
    public AjaxResult getInfo(@PathVariable("logId") Long logId)
    {
        DifferentLogging differentLogging = differentLoggingService.selectDifferentLoggingByLogId(logId);
        differentLoggingService.cheak(differentLogging);
        return success(differentLogging);
    }

    /**
     * 新增日志记录
     */
    @PreAuthorize("@ss.hasPermi('different:logging:add')")
    @Log(title = "日志记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DifferentLogging differentLogging)
    {
        differentLoggingService.cheak(differentLogging);
        return toAjax(differentLoggingService.insertDifferentLogging(differentLogging));
    }

    /**
     * 修改日志记录
     */
   @PreAuthorize("@ss.hasPermi('different:logging:edit')")
    @Log(title = "日志记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DifferentLogging differentLogging)
    {
        differentLoggingService.cheak(differentLoggingService.selectDifferentLoggingByLogId(differentLogging.getLogId()));
        return toAjax(differentLoggingService.updateDifferentLogging(differentLogging));
    }

    /**
     * 删除日志记录
     */
    @PreAuthorize("@ss.hasPermi('different:logging:remove')")
    @Log(title = "日志记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logIds}")
    public AjaxResult remove(@PathVariable Long[] logIds)
    {
        for (Long logId : logIds) {
            differentLoggingService.cheak(differentLoggingService.selectDifferentLoggingByLogId(logId));
        }
        return toAjax(differentLoggingService.deleteDifferentLoggingByLogIds(logIds));
    }
}
